<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @addList='fn'></TodoHeader>
    <!-- 列表区域 -->
    <TodoMain :list='list' @getId='getId'></TodoMain>
    <!-- 统计和清空 -->
    <TodoFooter :list='list' @clearList='clearList' v-show='list.length>0'></TodoFooter>
  </section>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";

export default {
  components: {
    TodoFooter,
    TodoHeader,
    TodoMain,
  },
  data() {
    return {
      list:JSON.parse(localStorage.getItem('list'))|| [
        { id: 1, name: "跑步一千米" },
        { id: 2, name: "游泳一千米" },
      ],
    };
  },
  methods:{
    fn(val){
      this.list.unshift({
        id:this.list.length+1,
        name:val
      })
    },
    getId(id){
     this.list= this.list.filter(item=>item.id!=id)
    },
    clearList(){
      this.list=[]
    }
  },
  watch:{
    list:{
      deep:true,
      handler(newval){
        localStorage.setItem('list',JSON.stringify(newval))
      }
    }
  }
};
</script>

<style>
</style>